Code coverage report for src/api.js

Statements: 59.09% (13 / 22)      Branches: 0% (0 / 2)      Functions: 50% (2 / 4)      Lines: 59.09% (13 / 22)      Ignored: none     

All files » src/ » api.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67    1 1   1                                                         27 27 27 27 27 27               1           1                     27    
var _ = require('./utility');
var helpers = require('./apiUtility');
 
var defaultOptions = {
  hostname: 'api.rollbar.com',
  path: '/api/1',
  search: null,
  version: '1',
  protocol: 'https:',
  port: 443
};
 
/**
 * Api is an object that encapsulates methods of communicating with
 * the Rollbar API.  It is a standard interface with some parts implemented
 * differently for server or browser contexts.  It is an object that should
 * be instantiated when used so it can contain non-global options that may
 * be different for another instance of RollbarApi.
 *
 * @param options {
 *    accessToken: the accessToken to use for posting items to rollbar
 *    endpoint: an alternative endpoint to send errors to
 *        must be a valid, fully qualified URL.
 *        The default is: https://api.rollbar.com/api/1
 *    proxy: if you wish to proxy requests provide an object
 *        with the following keys:
 *          host or hostname (required): foo.example.com
 *          port (optional): 123
 *          protocol (optional): https
 * }
 */
function Api(options, t, u, j) {
  this.options = options;
  this.transport = t;
  this.url = u;
  this.jsonBackup = j;
  this.accessToken = options.accessToken;
  this.transportOptions = _getTransport(options, u);
}
 
/**
 *
 * @param data
 * @param callback
 */
Api.prototype.postItem = function(data, callback) {
  var transportOptions = helpers.transportOptions(this.transportOptions, '/item/', 'POST');
  var payload = helpers.buildPayload(this.accessToken, data, this.jsonBackup);
  this.transport.post(this.accessToken, transportOptions, payload, callback);
};

Api.prototype.configure = function(options) {
  var oldOptions = this.oldOptions;
  this.options = _.extend(true, {}, oldOptions, options);
  this.transportOptions = _getTransport(this.options, this.url);
  if (this.options.accessToken !== undefined) {
    this.accessToken = this.options.accessToken;
  }
  return this;
};

function _getTransport(options, url) {
  return helpers.getTransportFromOptions(options, defaultOptions, url);
}
 
module.exports = Api;